package cn.edu.scau.cmi.wurihua.service;

import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;

import cn.edu.scau.cmi.wurihua.entity.Student;
import cn.edu.scau.cmi.wurihua.mapper.StudentMapper;
import cn.edu.scau.cmi.wurihua.util.MybatisUtil;

public class StudentService {
	private SqlSession sqlSession = MybatisUtil.getSession();
	private StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
	
	public Student selectService(int id) {
		return (Student)studentMapper.selectById(id);
		
	}
	
	public int insertService(Student s) {
		try {
			if(studentMapper.insert(s)>0) {
				sqlSession.commit();
				return 1;
			}				
			
		}catch (Exception e) {
			// TODO: handle exception
			sqlSession.rollback();
		}
		return 0;
	}
	public int deleteService(int id) {
		try {
			if(studentMapper.delete(id)>0) {
				sqlSession.commit();
				return 1;
			}
				
			
		}catch (Exception e) {
			// TODO: handle exception
			sqlSession.rollback();
		}
		return 0;
	}
	public int updateService(Student s) {
		try {
			if(studentMapper.update(s)>0) {
				sqlSession.commit();
				return 1;
			}
				
			
		}catch (Exception e) {
			// TODO: handle exception
			sqlSession.rollback();
		}
		return 0;
	}
	public int changeTutorService(int sId,int tId) {
		try {
			if(studentMapper.changeTutor(sId, tId)>0) {
				sqlSession.commit();
				return 1;
			}
				
			
		}catch (Exception e) {
			// TODO: handle exception
			sqlSession.rollback();
		}
		return 0;

	}
}
